/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package koncowka;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author root
 */
public class Interpreter extends Thread {
    
    private Map<String,Map<String,Regula>> mapaRegul;
    private List<Ap> listaAp;
    private Boolean stop;
    private static long SLEEPTIME = 20000;

    
    @Override
    public synchronized void run() {
        try {
            super.run();
            stop = false;
            this.wait(SLEEPTIME);
            while(!stop)
            {
                mapaRegul = Koncowka.getMapaRegul();
                if(mapaRegul==null)
                    mapaRegul = new HashMap<String,Map<String,Regula>>();
                listaAp = new ArrayList<Ap>();
                for (Ap ap : Koncowka.getListaAp()) {
                    Ap tmp = new Ap(ap.getBSSID(), ap.getFirstSeen(), ap.getLastSeen(), ap.getChanell(), ap.getSpeed(), ap.getPower(), ap.getESSID(), ap.getKey());
                    ArrayList<Klient> listaKl = new ArrayList<Klient>();
                    for (Klient kl :ap.getListaKlientow()) {
                        listaKl.add(new Klient(kl.getMAC(), kl.getFirstSeen(), kl.getLastSeen(), kl.getPower(), kl.getApBSSID()));
                    }
                    tmp.setListaKlientow(listaKl);
                    listaAp.add(tmp);
                }
                //System.out.println("Lista ap liczy " + Koncowka.getListaAp().size());
                for (Ap ap : listaAp) {
                    for (Klient klient : ap.getListaKlientow()) {
                        //System.out.println("Sprawdzam " + ap.getBSSID());
                        if(!mapaRegul.containsKey(ap.getBSSID()))
                        {
//                            if(Koncowka.getCzyBlokowacAp())
//                            {
//                                mapaRegul.put(ap.getBSSID(), new HashMap<String, Regula>());
//                                Map<String, Regula> mapa = mapaRegul.get(ap.getBSSID());
//                                mapa.put(klient.getMAC(),new Regula(ap.getBSSID(), klient.getMAC(), Boolean.TRUE));
//                                mapa.get(klient.getMAC()).setBlokada(new BlokadaKlienta(ap.getBSSID(), klient.getMAC()));
//                            }else
//                            {
//                                mapaRegul.put(ap.getBSSID(), new HashMap<String, Regula>());
//                                Map<String, Regula> mapa = mapaRegul.get(ap.getBSSID());
//                                mapa.put(klient.getMAC(),new Regula(ap.getBSSID(), klient.getMAC(), Boolean.FALSE));
//                            }
                        }else
                        {
                            Map<String, Regula> mapa = mapaRegul.get(ap.getBSSID());
                            if(!mapa.containsKey(klient.getMAC()))
                            {
                                if(!mapa.containsKey("FF:FF:FF:FF:FF:FF") && Koncowka.getCzyBlokowacKlienta())
                                {
                                    mapa.put(klient.getMAC(),new Regula(ap.getBSSID(), klient.getMAC(), Boolean.TRUE));
                                    mapa.get(klient.getMAC()).setBlokada(new BlokadaKlienta(ap.getBSSID(), klient.getMAC()));
                                }else
                                {
                                    if(mapa.containsKey("FF:FF:FF:FF:FF:FF"))
                                    {
                                        if(mapa.get("FF:FF:FF:FF:FF:FF").getCzyBlokowac())
                                        {
                                            mapa.put(klient.getMAC(),new Regula(ap.getBSSID(), klient.getMAC(), Boolean.TRUE));
                                            mapa.get(klient.getMAC()).setBlokada(new BlokadaKlienta(ap.getBSSID(), klient.getMAC()));
                                        }else
                                        {
                                            mapa.put(klient.getMAC(),new Regula(ap.getBSSID(), klient.getMAC(), Boolean.FALSE));
                                        }
                                    }else
                                    {
                                        mapa.put(klient.getMAC(),new Regula(ap.getBSSID(), klient.getMAC(), Boolean.FALSE));
                                    }
                                }
                            }else
                            {
                                Regula regula = mapa.get(klient.getMAC());
                                if(regula.getCzyBlokowac())
                                {
                                    if(regula.getBlokada()==null)
                                        regula.setBlokada(new BlokadaKlienta(ap.getBSSID(), klient.getMAC()));
                                }
                                else                                    
                                {
                                    if(regula.getBlokada()!=null)
                                    {
                                        regula.getBlokada().stop();
                                        regula.setBlokada(null);
                                    }

                                }
                            }
                        }
                    }
                }

                this.wait(SLEEPTIME);
            }
        } catch (InterruptedException ex) {
            Logger.getLogger(Interpreter.class.getName()).log(Level.SEVERE, null, ex);
        }
        
    }

    /**
     *
     */
    public void koniec() {

        this.stop = true;
        if(mapaRegul!=null && !mapaRegul.isEmpty())
        {
            for (Map<String, Regula> mapaKl : mapaRegul.values() ) {
                for (Regula regula : mapaKl.values()) {
                    if(regula.getBlokada()!=null){
                        regula.getBlokada().stop();
                        regula.setBlokada(null);
                    }
                }
            }
        }
    }

}
